package com.tul.exercise.base.java8.stream.base;

import com.tul.cusa.common.util.RandomUtil;
import com.tul.exercise.base.java8.stream.base.Apple;
import com.tul.exercise.base.utils.MathUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 使用Apple类演示stream使用实例
 *
 * @author tuliang
 */
public class AppleTest {

    @Test
    @DisplayName("filter方法")
    public void test01() {

        List<Apple> apples = AppleHelper.createApples(100);

        // 根据名称筛选
        Predicate<Apple> predicate = (apple) -> apple.getName() == "apple";
        List<Apple> list = apples.stream().filter(predicate).collect(Collectors.toList());
        System.out.println(list);

        // 根据重量筛选
        predicate = (Apple apple) -> apple.getWeight() > 300;
        list.clear();
        list = apples.stream().filter(predicate).collect(Collectors.toList());
        System.out.println(list);
    }

    @Test
    @DisplayName("排序")
    public void test02() {
        List<Apple> apples = AppleHelper.createApples(10);

        // 根据重量排序
        Comparator<Apple> comparator = (a1, a2) -> a1.getWeight().compareTo(a2.getWeight());
        List<Apple> list = apples.stream().sorted(comparator).collect(Collectors.toList());
        list.forEach(System.out::println);

        // 更简洁的的写法
        Function<Apple,Double> f = Apple::getWeight;
        List<Apple> list2 = apples.stream()
                .sorted(Comparator.comparing(f))
                .collect(Collectors.toList());
        list2.forEach(System.out::println);



    }

    @Test
    @DisplayName("线程")
    public void test03() {

        // 传统方式创建线程
        Thread t = new Thread("传统方式线程") {
            public void run() {
                int i = 28;
                while (i-- > 0) {
                    System.out.println(Thread.currentThread().getName() + " i=" + i);
                }

            }
        };
        t.start();


        // lambda方式创建线程
        Thread t2 = new Thread(() -> {
            int i = 28;
            while (i-- > 0) {
                System.out.println(Thread.currentThread().getName() + " i=" + i);
            }
        });
        t2.setName("java8 Lambda线程");
        t2.start();


        // 更加lambda方式
        Runnable r1 = () -> {
            int i = 28;
            while (i-- > 0) {
                System.out.println(Thread.currentThread().getName() + " i=" + i);
            }
        };

        Thread t3 = new Thread(r1);
        t3.setName("");
        t3.start();

    }

    @Test
    @DisplayName("函数式接口")
    public void test04() {

        // Predicate接口
        Predicate<Apple> isColor = (Apple a) -> a.getColor().equals("red");
        List<Apple> list = AppleHelper.createApples(29).stream()
                .filter(isColor)
                .collect(Collectors.toList());

        // Consumer接口
        Consumer<Apple> doSomething = (Apple apple) -> {
            System.out.println(apple.getName());
        };
        AppleHelper.createApples(20).stream()
                .forEach(doSomething);


        // Supplier接口
        Supplier<Apple> newApple = () -> {
            return new Apple();
        };

        // 函数接口
        Function<Double, Double> sin = (Double x) -> {
            return Math.sin(x);
        };
        //List<Integer> numbers = IntStream.rangeClosed(1, 1000).collect(Collectors.toList())
        Double result = sin.apply(3d).doubleValue();
        System.out.println(result);


    }


    /**
     * Apple帮助类
     */
    public static class AppleHelper {

        public static final List<String> NAMES = Stream.of("apple", "apple1",
                "apple2", "apple3", "apple4").collect(Collectors.toList());

        public static final List<String> COLORS = Stream.of("red",
                "green", "white", "blue").collect(Collectors.toList());

        /**
         * 创建多个Apple实例
         *
         * @param num 个数
         * @return List
         */
        public static List<Apple> createApples(int num) {
            List<Apple> list = new ArrayList<>();
            for (int i = 0; i < num; i++) {
                list.add(createApple());
            }
            return list;
        }

        /**
         * 创建Apple对象
         *
         * @return Apple对象
         */
        public static Apple createApple() {
            Apple apple = new Apple();
            apple.setName(RandomUtil.getRandomItem(NAMES));
            apple.setColor(RandomUtil.getRandomItem(COLORS));
            apple.setWeight(Double.valueOf(MathUtils.getRandomInt(0, 1000)));
            return apple;
        }
    }
}
